package org.andao.commons.lucene.core.assembler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.andao.commons.lucene.core.mapper.BeanMapper;



/**
 * （搜索）索引装配器，基于solr的实现
 * 
 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
 * 
 */
public class SolrBeanListAssembler implements BeanListAssembler {

	/**
	 * bean映射器（接口）bean2Index
	 */
	private BeanMapper beanMapper;

	/**
	 * 构造注入
	 * 
	 * @param beanMapper
	 */
	public SolrBeanListAssembler(BeanMapper beanMapper) {
		this.beanMapper = beanMapper;
	}

	/**
	 * 装配数据，bean2Index
	 * 
	 * @param resultList
	 *            搜索结果集
	 * @param key
	 *            索引主键
	 * @return
	 */
	@Override
	public Object assembleData(List<?> resultList, String key) {

		List<Object> results = new ArrayList<Object>();
		int rowNum = 0;//行号
		Iterator<?> it = resultList.iterator();
		while (it.hasNext()) {
			results.add(this.beanMapper.mapBean(it.next(), rowNum++));
		}
		return results;
	}
}
